package com.simpleaccount.Graph;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.simpleaccount.Common.Utils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017/7/19.
 */

public class MyListViewAdapter extends BaseSwipeAdapter{
    private HashMap<String,Float> moneys;
    private Context context;
    private List<Category> categories=new ArrayList<>();
    private SwipeLayout swipeLayout;

    public MyListViewAdapter(Context context,HashMap<String,Float> rawData,List<Category> categories){
        this.context=context;
        moneys=rawData;
        for(Category cate:categories){
            if(moneys.get(cate.getName())!=0f){
                this.categories.add(cate);
            }
        }
        Collections.sort(this.categories,new CateCmptr(categories.get(0).getType()));
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.sl_entry;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View view =LayoutInflater.from(context).inflate(R.layout.base_search_item,null);
        return view;
    }

    @Override
    public void fillValues(int position, View convertView) {
        swipeLayout =(SwipeLayout)convertView.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.close();
        swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
        swipeLayout.addDrag(SwipeLayout.DragEdge.Right,convertView.findViewById(R.id.ll_hidden_menu));


        Category category=categories.get(position);
        MyViewHolder holder=new MyViewHolder(convertView);
        Utils.setImage(holder.getIvCategory(),category.getName());
        holder.getTvMoney().setText(String.format(Locale.CHINA,"%.2f",moneys.get(category.getName())));
        holder.getTvCategory().setText(category.getName());
        holder.getTvPercentage().setText((String.format(Locale.CHINA,"%.2f",sumPercentage(category))+"%"));
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    private Float sumPercentage(Category category){
        Float sum=0f;
        for(Category cate:categories){
            sum+=moneys.get(cate.getName());
        }
        return moneys.get(category.getName())/sum*100;
    }

    private final class CateCmptr implements Comparator<Category>{
        int type=0;
        public CateCmptr(int type){
            this.type=type;
        }
        @Override
        public int compare(Category o1, Category o2) {
            int res=0;
            Float f1=moneys.get(o1.getName());
            Float f2=moneys.get(o2.getName());
            if(f1<f2){
                if(type==1){
                    res=1;
                }else {
                    res=-1;
                }
            }
            if(f1>f2){
                if(type==1){
                    res=-1;
                }else {
                    res=1;
                }
            }
            return res;
        }
    }
}
